package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    // 新增菜品
    @PostMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result add(@RequestBody DishDTO dishDTO){
        log.info("新增菜品,接收到的参数是{}",dishDTO);
        dishService.add(dishDTO);
        return Result.success();
    }

    // 菜品分页查询
    @GetMapping("/page")
    public Result pageQuery(DishPageQueryDTO dto){
        log.info("菜品分页查询,接收到的参数是{}",dto);
        PageResult dishlist =  dishService.pageQuery(dto);
        return Result.success(dishlist) ;
    }

    // 批量删除菜品
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result deleteByIds(@RequestParam("ids") List<Long> ids){
        log.info("批量删除菜品,接收到的参数是{}",ids);
        Integer i = dishService.deleteByIds(ids);
        if (i == 1){
            return Result.success();
        }
        return Result.error("该菜品不能删除");
    }

    // 根据id查询菜品
    @GetMapping("/{dishId}")
    public Result selectById(@PathVariable("dishId") Long dishId){
        log.info("根据id查询菜品,接收到的参数是{}",dishId);
        DishVO dishVo = dishService.selectById(dishId);
        return Result.success(dishVo);
    }

    // 修改菜品
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品,接收到的参数是{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    // 修改菜品状态
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateByStatus (@PathVariable("status") Integer status,Long id){
        log.info("修改菜品状态,接收到的参数是{}",status);
        DishDTO dishDTO = new DishDTO();
        dishDTO.setStatus(status);
        dishDTO.setId(id);
        dishService.update(dishDTO);
        return Result.success();
    }

    // 根据分类id查询菜品
    @GetMapping("/list")
    public Result selectByCategoryId(Integer categoryId){
        log.info("根据分类id查询菜品,接收到的参数是{}",categoryId);
        List<Dish> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }

}
